<!--
  * OCR配置检查页面
  *
  * @Author:    1024创新实验室-主任：卓大
  * @Date:      2024-01-01 00:00:00
  * @Wechat:    zhuda1024
  * @Email:     lab1024@163.com
  * @Copyright  1024创新实验室 （ https://1024lab.net ），Since 2012
-->
<template>
  <div class="ocr-check-container">
    <a-card title="OCR配置检查" :bordered="false">
      <template #extra>
        <a-button type="primary" @click="checkOcrConfig" :loading="loading">
          检查OCR配置
        </a-button>
      </template>

      <a-descriptions title="OCR配置状态" :column="1" bordered>
        <a-descriptions-item label="配置状态">
          <a-tag :color="ocrStatus.available ? 'green' : 'red'">
            {{ ocrStatus.available ? '正常' : '异常' }}
          </a-tag>
        </a-descriptions-item>
        <a-descriptions-item label="默认语言">
          {{ ocrStatus.defaultLanguage || '未设置' }}
        </a-descriptions-item>
        <a-descriptions-item label="可用语言包">
          <a-tag v-for="lang in ocrStatus.availableLanguages" :key="lang" color="blue">
            {{ lang }}
          </a-tag>
        </a-descriptions-item>
      </a-descriptions>

      <a-divider />

      <a-collapse v-model:activeKey="activeKeys">
        <a-collapse-panel key="errors" header="错误信息" v-if="ocrStatus.errors.length > 0">
          <a-alert
            v-for="(error, index) in ocrStatus.errors"
            :key="index"
            :message="error"
            type="error"
            show-icon
            style="margin-bottom: 8px"
          />
        </a-collapse-panel>

        <a-collapse-panel key="infos" header="信息提示" v-if="ocrStatus.infos.length > 0">
          <a-alert
            v-for="(info, index) in ocrStatus.infos"
            :key="index"
            :message="info"
            type="info"
            show-icon
            style="margin-bottom: 8px"
          />
        </a-collapse-panel>

        <a-collapse-panel key="config" header="PDF配置信息">
          <a-descriptions :column="2" bordered>
            <a-descriptions-item label="Tessdata路径">
              {{ pdfConfig.tessdataPath }}
            </a-descriptions-item>
            <a-descriptions-item label="文件大小限制">
              {{ pdfConfig.uploadMaxFileSize }}MB
            </a-descriptions-item>
            <a-descriptions-item label="线程池大小">
              {{ pdfConfig.ocr.threadPoolSize }}
            </a-descriptions-item>
            <a-descriptions-item label="图片DPI">
              {{ pdfConfig.ocr.imageDpi }}
            </a-descriptions-item>
            <a-descriptions-item label="图片预处理">
              {{ pdfConfig.ocr.enableImagePreprocessing ? '启用' : '禁用' }}
            </a-descriptions-item>
            <a-descriptions-item label="多语言识别">
              {{ pdfConfig.ocr.enableMultiLanguage ? '启用' : '禁用' }}
            </a-descriptions-item>
            <a-descriptions-item label="LSTM引擎">
              {{ pdfConfig.ocr.enableLstmEngine ? '启用' : '禁用' }}
            </a-descriptions-item>
            <a-descriptions-item label="最小文本长度">
              {{ pdfConfig.ocr.minTextLength }}
            </a-descriptions-item>
          </a-descriptions>
        </a-collapse-panel>
      </a-collapse>

      <a-divider />

      <a-alert
        message="OCR配置说明"
        description="
          1. 确保已安装Tesseract OCR引擎<br>
          2. 确保tessdata目录包含必要的语言包文件（如chi_sim.traineddata、eng.traineddata）<br>
          3. 检查tessdata路径配置是否正确<br>
          4. 如果OCR识别失败，请检查PDF文件是否为图片格式
        "
        type="info"
        show-icon
      />
    </a-card>
  </div>
</template>

<script setup>
  import { ref, onMounted } from 'vue';
  import { message } from 'ant-design-vue';
  import { pdfApi } from '/@/api/business/pdf-api';

  const loading = ref(false);
  const activeKeys = ref(['errors', 'infos', 'config']);

  const ocrStatus = ref({
    available: false,
    defaultLanguage: '',
    availableLanguages: [],
    errors: [],
    infos: []
  });

  const pdfConfig = ref({
    tessdataPath: '',
    uploadMaxFileSize: 50,
    ocr: {
      threadPoolSize: 4,
      imageDpi: 300,
      enableImagePreprocessing: true,
      enableMultiLanguage: true,
      minTextLength: 5,
      enableLstmEngine: true
    }
  });

  // 检查OCR配置
  const checkOcrConfig = async () => {
    try {
      loading.value = true;
      const res = await pdfApi.checkOcrConfig();
      ocrStatus.value = res.data;
      message.success('OCR配置检查完成');
    } catch (e) {
      message.error('OCR配置检查失败: ' + e.message);
    } finally {
      loading.value = false;
    }
  };

  // 获取PDF配置
  const getPdfConfig = async () => {
    try {
      const res = await pdfApi.getPdfConfig();
      pdfConfig.value = res.data;
    } catch (e) {
      console.error('获取PDF配置失败:', e);
    }
  };

  onMounted(() => {
    getPdfConfig();
    checkOcrConfig();
  });
</script>

<style scoped>
  .ocr-check-container {
    padding: 24px;
  }
</style> 